﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;

namespace GameSPK
{
    class Helper
    {
        public static String smodaucauhoi = "Câu";
        public static String dapan = "TRẢ LỜI";
        public static int maxNum = 100;
        
        public static ObservableCollection<ICauHoi> LuuCauHoiPhan1(String noidungfile, int phanthi, String maCD)
        {
            String[] chuoicat;
            int socau = 0;
            ObservableCollection<ICauHoi> taphopcauhoi = new ObservableCollection<ICauHoi>();
            GoiCauHoi goicauhoi;
            chuoicat = new string[Helper.maxNum];
            for (int index = 0; index < Helper.maxNum; index++)
            {
                chuoicat[index] = Helper.smodaucauhoi + " " + (index + 1) + ": ";
            }
            String[] catDapan = new string[1];
            catDapan[0] = Helper.dapan;

            string[] s = noidungfile.Split(chuoicat, Helper.maxNum + 10, StringSplitOptions.RemoveEmptyEntries);
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            goicauhoi = new GoiCauHoi();
            if (s.Length >= 1)
            {
                goicauhoi.MaGoi = Helper.GenerateKey();
                goicauhoi.NgayNhap = DateTime.Now;
                db.GoiCauHois.InsertOnSubmit(goicauhoi);
                db.SubmitChanges();
            }


            if (phanthi == 1)
            {
                foreach (String scon in s)
                {
                    try
                    {

                        String[] cauhoi = scon.Split(catDapan, 10, StringSplitOptions.None);

                        if (cauhoi.Length <= 1)
                        {
                            continue;
                        }
                        CauHoi cauhoimoi = new CauHoi();
                        cauhoimoi.MaCauHoi = Helper.GenerateKey() + "" + socau++;
                        cauhoimoi.CauHoi1 = cauhoi[0];
                        cauhoimoi.DapAnDung = cauhoi[1];
                        cauhoimoi.MaCD = maCD;
                        cauhoimoi.MaPT = phanthi;
                        cauhoimoi.GoiCauHoi = goicauhoi;
                        db.CauHois.InsertOnSubmit(cauhoimoi);
                        taphopcauhoi.Add(new dataCauHoi(cauhoimoi, true));
                    }
                    catch
                    {
                    }
                }
            }
            else if(phanthi == 2 || phanthi==5)
            {
                foreach (String scon in s)
                {
                    try
                    {

                        CauHoi cauhoimoi = new CauHoi();
                        cauhoimoi.MaCauHoi = Helper.GenerateKey() + "" + socau++;
                        cauhoimoi.CauHoi1 = scon;
                        cauhoimoi.DapAnDung = "";
                        cauhoimoi.MaCD = maCD;
                        cauhoimoi.MaPT = phanthi;
                        cauhoimoi.GoiCauHoi = goicauhoi;
                        db.CauHois.InsertOnSubmit(cauhoimoi);
                        taphopcauhoi.Add(new dataCauHoi(cauhoimoi, true));
                    }
                    catch
                    {
                    }
                }
            }
            try
            {
                db.SubmitChanges();

                return taphopcauhoi;
            }
            catch(Exception ex)
            {
                return null;
            }
        }
       
        public static string ImageBackDefault = "images/default.jpg";
        public static String GenerateKey()
        {
            String key = null;
            key = DateTime.Now.Year+""+DateTime.Now.Month+"" + DateTime.Now.Day+"" +DateTime.Now.Hour + "" + 
                DateTime.Now.Minute + "" + DateTime.Now.Second;
                
            return key;
        }
        //luu cau hinh phan chung xuong file xml
        public static void SetupGenerel(Label lblBanner,Brush Background)
        {
            CauHinhChung cauhinh = CauHinhChung.getInstance();
            //var initialLocation = lblBanner.TranslatePoint(new Point(0, 0), this);
            //lblBanner.RenderTransform = new TranslateTransform(cauhinh.mBanner.left - initialLocation.X, cauhinh.mBanner.top - initialLocation.Y);
            lblBanner.Margin = new Thickness(cauhinh.mBanner.left, cauhinh.mBanner.top, 0, 0);

            lblBanner.Width = cauhinh.mBanner.width;
            lblBanner.Height = cauhinh.mBanner.height;
            lblBanner.Content = cauhinh.mBanner.content;
            lblBanner.FontSize = cauhinh.mBanner.fontsize;
            Color backcolor = (Color)ColorConverter.ConvertFromString(cauhinh.mBanner.background);
            SolidColorBrush brush;
            brush = new SolidColorBrush(backcolor);
            lblBanner.Background = brush;
            Color forecolor = (Color)ColorConverter.ConvertFromString(cauhinh.mBanner.foreground);
            SolidColorBrush brushfore;
            brushfore = new SolidColorBrush(forecolor);
            lblBanner.Foreground = brushfore;
            ImageBrush myBrush = new ImageBrush();
            myBrush.ImageSource =
                   new BitmapImage(new Uri(cauhinh.imagebackground, UriKind.RelativeOrAbsolute));
            Background = myBrush;
        }

        
    }
    public struct Banner
    {
        public double left;
        public double top;
        public double width;
        public double height;
        public string foreground;
        public string background;
        public int fontsize;
        public string content;
    }
    public class CauHinhChung
    {
        private static CauHinhChung instance;
        private static object syncLock = new object();
        public Banner mBanner;
        public string imagebackground;
        public static CauHinhChung getInstance()
        {
            if (instance == null)
            {
                lock (syncLock)
                {
                    if (instance == null)
                    {
                        instance = new CauHinhChung();
                    }
                }
            }

            return instance;
        }
        private CauHinhChung()
        {
            string folder = "layout";
            if (File.Exists(folder + "/mainlayout.xml"))
            {
                XElement layout = XElement.Load(folder + "/mainlayout.xml");
                var elements = layout.Elements().Select(
                    el => new
                    {
                        Name = el.Name,
                        Top = (double)el.Attribute("Top"),
                        Left = (double)el.Attribute("Left"),
                        Width = (double)el.Attribute("Width"),
                        Height = (double)el.Attribute("Height"),
                        FontSize = (int)el.Attribute("FontSize"),
                        CololorBanner = (string)el.Attribute("CololorBanner"),
                        CololorBackBanner = (string)el.Attribute("CololorBackBanner")
                    })
                    ;
                var bannerelement = elements.Where(bn => bn.Name == "Banner").Single();
                mBanner.top = bannerelement.Top;
                mBanner.left = bannerelement.Left;
                mBanner.width = bannerelement.Width;
                mBanner.height = bannerelement.Height;
                mBanner.fontsize = bannerelement.FontSize;
                mBanner.background = bannerelement.CololorBackBanner;
                mBanner.foreground = bannerelement.CololorBanner;
                DBTroChoiDataContext db = new DBTroChoiDataContext();
                var banner = from bn in db.Generals
                             where bn.Chon == true
                             select bn;
                mBanner.content = banner.Single().Banner;
                this.imagebackground = banner.Single().ImageBackground;
                //Messagebox.Show();
            }
        }
    }
}

